package com.eight.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eight.common.pojo.AclRole;
import com.eight.common.vo.Result;
import com.eight.system.service.RoleService;
import com.eight.system.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.management.relation.Role;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 向文豪
 * @program: eight-education-parent
 * @description:
 * @date 2023/5/24 20:50
 */
@RestController
@RequestMapping("/system/role")
@Api("角色权限管理")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/getByUserId/{userId}")
    @ApiOperation(value = "查询用户角色 ")
    public Result getByUserId(@PathVariable String userId){
        List<AclRole> all = roleService.findAll();
        List<AclRole> roles = roleService.findByUserId(userId);
        List<String> roleids = roles.stream().map(item -> item.getId()).collect(Collectors.toList());
        Map<String,Object> map=new HashMap<>();
        map.put("all",all);
        map.put("roles",roleids);
        return Result.success(map);
    }
    @PostMapping("confirm")
    @ApiOperation("确认分配角色")
    public Result confirm( String userId, String[] roleIds){
        Integer row=roleService.confirm(userId,roleIds);
        return Result.success();
    }

    @PostMapping("list/{currentPage}/{pageSize}")
    @ApiOperation(value = "分页条件查询角色")
    public Result list(@PathVariable Integer currentPage,@PathVariable Integer pageSize,@RequestBody RoleVo roleVo){
        Page<AclRole> page= roleService.findPage(currentPage,pageSize,roleVo);
        return Result.success(page);
    }

    @ApiOperation("修改角色状态")
    @PostMapping("/updateRoleStatusByid")
    public Result updateRoleStatusByid( @RequestParam  String id ,@RequestParam  Integer status ){
        int i = roleService.updateRoleStatusByid(id, status);
        return Result.success();
    }

    @ApiOperation("根据ID删除角色")
    @DeleteMapping("/deleteRoleById/{id}")
    public Result deleteRoleById(@PathVariable String id){
        int i = roleService.deleteRoleById(id);
        return Result.success();
    }
    @ApiOperation("添加角色")
    @PostMapping("/addRoleByRole")
    public Result addRoleByRole(@RequestBody AclRole aclRole){
        int i = roleService.addRoleByRle(aclRole);
        return Result.success(i);
    }
    @ApiOperation("修改角色")
    @PostMapping("/updateRoleByRole")
    public Result updateRoleByRole(@RequestBody AclRole aclRole){
        int updateRoleByRole = roleService.updateRoleByRole(aclRole);
        return Result.success(updateRoleByRole);
    }
}
